package com.dehong.controller;

import com.dehong.model.ContractC;
import com.dehong.model.ContractProductC;
import com.dehong.model.ExportC;
import com.dehong.service.ContractCService;
import com.dehong.service.ContractProductCService;
import com.dehong.service.ExportCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/export")
public class ExportController {

    @Autowired
    private ExportCService exportCService;

    @Autowired
    private ContractCService contractCService;
    @Autowired
    private ContractProductCService contractProductCService;
    @RequestMapping("/Goexport")
    public  String Goexport (Model model){

       List<ExportC> lists =   exportCService.getAll();

        model.addAttribute("dataList",lists);

        return  "/pages/basicinfo/export/jExportList";

    }

    @RequestMapping("/insert")
    public  String insert (String[] id,Model model){
        for (int i = 0;i<id.length;i++){
            System.out.println(id[i]);
            ContractC contractC = contractCService.selectByPrimaryKey(id[i]);
            ExportC exportC = new ExportC();
            exportC.setExportId(UUID.randomUUID()+"");
            exportC.setInputDate(new Date());
            exportC.setCustomerContract(contractC.getContractNo());
            exportC.setConsignee(contractC.getOfferor());
            exportC.setState(contractC.getState());
            exportCService.insert(exportC);
        }

        List<ExportC> lists =  exportCService.getAll();
        for (ExportC e :
                lists) {
            System.out.println("================2");
            System.out.println(e.toString());
        }
        model.addAttribute("dataList",lists);
        return  "/pages/basicinfo/export/jExportList";

    }
    //修改页面
    @RequestMapping("goupdateExport")
    public  String goupdateExport(ExportC exportC, Model model){
        System.out.println(exportC.getExportId());
        ExportC selectexportid = exportCService.selectexportid(exportC);
        String[] split = selectexportid.getContractIds().split(",");
        List<ContractProductC> CList=new ArrayList<>();

        for (int i = 0; i <split.length ; i++) {
            List<ContractProductC> contractProductCS = contractProductCService.selectBycid(split[i]);
            if (null!=contractProductCS){
            for (ContractProductC contractP : contractProductCS) {
                        CList.add(contractP);
            }
            }
        }
        for (ContractProductC c : CList) {
            System.out.println(c);
        }
        model.addAttribute("obj",selectexportid);
        model.addAttribute("mRecordData",CList);
        return  "/pages/basicinfo/export/jExportUpdate";
    }
    @RequestMapping("updateExport")
    public  String updateExport(ExportC exportC){
        int i = exportCService.updateByPrimaryKeySelective(exportC);
        return "/pages/basicinfo/export/jExportList";
    }
}

